Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
@contentful/rich-text-html-renderer
Advanced tools
HTML renderer for the Contentful rich text field type.
@contentful/rich-text-html-renderer is an npm package that allows you to convert Contentful's rich text document format into HTML. This is particularly useful for rendering rich text content stored in Contentful CMS into web pages.
Rendering Rich Text to HTML
This feature allows you to convert a Contentful rich text document into an HTML string. The code sample demonstrates how to use the `documentToHtmlString` function to achieve this.
const { documentToHtmlString } = require('@contentful/rich-text-html-renderer');
const richTextDocument = {
nodeType: 'document',
content: [
{
nodeType: 'paragraph',
content: [
{
nodeType: 'text',
value: 'Hello, world!',
marks: [],
data: {}
}
],
data: {}
}
],
data: {}
};
const htmlString = documentToHtmlString(richTextDocument);
console.log(htmlString); // Outputs: <p>Hello, world!</p>
Customizing HTML Output
This feature allows you to customize the HTML output by providing options to the `documentToHtmlString` function. The code sample shows how to add a custom class to paragraph elements.
const { documentToHtmlString } = require('@contentful/rich-text-html-renderer');
const richTextDocument = {
nodeType: 'document',
content: [
{
nodeType: 'paragraph',
content: [
{
nodeType: 'text',
value: 'Hello, world!',
marks: [],
data: {}
}
],
data: {}
}
],
data: {}
};
const options = {
renderNode: {
'paragraph': (node, next) => `<p class="custom-class">${next(node.content)}</p>`
}
};
const htmlString = documentToHtmlString(richTextDocument, options);
console.log(htmlString); // Outputs: <p class="custom-class">Hello, world!</p>
The `rich-text-to-html` package provides similar functionality for converting rich text documents to HTML. It offers a straightforward API but may not be as tightly integrated with Contentful's specific rich text format as @contentful/rich-text-html-renderer.
The `draft-js-export-html` package is used to convert Draft.js editor content to HTML. While it is not specifically designed for Contentful's rich text format, it offers robust customization options and can be adapted for various rich text needs.
The `html-to-draftjs` package converts HTML to Draft.js content state. It serves the opposite purpose but can be used in conjunction with Draft.js to achieve similar results. It is not specifically tailored for Contentful's rich text format.
HTML renderer for the Contentful rich text field type.
Using npm:
npm install @contentful/rich-text-html-renderer
Using yarn:
yarn add @contentful/rich-text-html-renderer
import { documentToHtmlString } from '@contentful/rich-text-html-renderer';
const document = {
nodeType: 'document',
content: [
{
nodeType: 'paragraph',
content: [
{
nodeType: 'text',
value: 'Hello world!',
marks: [],
},
],
},
],
};
documentToHtmlString(document); // -> <p>Hello world!</p>
import { documentToHtmlString } from '@contentful/rich-text-html-renderer';
const document = {
nodeType: 'document',
content: [
{
nodeType: 'paragraph',
content: [
{
nodeType: 'text',
value: 'Hello',
marks: [{ type: 'bold' }],
},
{
nodeType: 'text',
value: ' world!',
marks: [{ type: 'italic' }],
},
],
},
],
};
documentToHtmlString(document); // -> <p><b>Hello</b><u> world!</u></p>
You can also pass custom renderers for both marks and nodes as an optional parameter like so:
import { BLOCKS, MARKS } from '@contentful/rich-text-types';
import { documentToHtmlString } from '@contentful/rich-text-html-renderer';
const document = {
nodeType: 'document',
data: {},
content: [
{
nodeType: 'paragraph',
data:{},
content: [
{
nodeType: 'text',
value: 'Hello',
marks: [{ type: 'bold' }],
data: {}
},
{
nodeType: 'text',
value: ' world!',
marks: [{ type: 'italic' }]
data: {}
},
],
},
]
};
const options = {
renderMark: {
[MARKS.BOLD]: text => `<custom-bold>${text}<custom-bold>`
},
renderNode: {
[BLOCKS.PARAGRAPH]: (node, next) => `<custom-paragraph>${next(node.content)}</custom-paragraph>`
}
}
documentToHtmlString(document, options);
// -> <custom-paragraph><custom-bold>Hello</custom-bold><u> world!</u></custom-paragraph>
Last, but not least, you can pass a custom rendering component for an embedded entry:
import { BLOCKS } from '@contentful/rich-text-types';
import { documentToHtmlString } from '@contentful/rich-text-html-renderer';
const document = {
nodeType: 'document',
data: {},
content: [
{
nodeType: 'embedded-entry-block',
data: {
target: (...)Link<'Entry'>(...);
},
},
]
};
const options = {
renderNode: {
[BLOCKS.EMBEDDED_ENTRY]: (node) => `<custom-component>${customComponentRenderer(node)}</custom-component>`
}
}
documentToHtmlString(document, options);
// -> <custom-component>(...)Link<'Entry'>(...)</custom-component>
The renderNode
keys should be one of the following BLOCKS
and INLINES
properties as defined in @contentful/rich-text-types
:
BLOCKS
DOCUMENT
PARAGRAPH
HEADING_1
HEADING_2
HEADING_3
HEADING_4
HEADING_5
HEADING_6
UL_LIST
OL_LIST
LIST_ITEM
QUOTE
HR
EMBEDDED_ENTRY
EMBEDDED_ASSET
INLINES
EMBEDDED_ENTRY
(this is different from the BLOCKS.EMBEDDED_ENTRY
)HYPERLINK
ENTRY_HYPERLINK
ASSET_HYPERLINK
The renderMark
keys should be one of the following MARKS
properties as defined in @contentful/rich-text-types
:
BOLD
ITALIC
UNDERLINE
CODE
SUPERSCRIPT
SUBSCRIPT
FAQs
HTML renderer for the Contentful rich text field type.
The npm package @contentful/rich-text-html-renderer receives a total of 127,646 weekly downloads. As such, @contentful/rich-text-html-renderer popularity was classified as popular.
We found that @contentful/rich-text-html-renderer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.